$(function () {

  function disableSubmitButtons(on) {
    $('#form').data('formValidation').disableSubmitButtons(on);
  }

  $('#form')
      .formValidation({
        message: 'This value is not valid',
        icon: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
          password: {
            validators: {
              notEmpty: {
                message: gettext('The password is required and can\'t be empty')
              }
            }
          },
          password_again: {
            validators: {
              notEmpty: {
                message: gettext('The password is required and can\'t be empty')
              },
              identical: {
                field: 'password',
                message: gettext('The password and its confirm are not the same')
              }
            }
          }
        }
      })
      .on('success.field.fv', function (e, data) {
          disableSubmitButtons(data.fv.getInvalidFields().length > 0);
      })
      .on('success.form.fv', function (e) {
          e.preventDefault();
          $.requestJSON({
            url: Urls.resolve('accounts_api_v1:reset_password'),
            method: "PATCH",
            data: {'token': $.getURLParam('token'), 'new_password': $("#password").val()},
            onSuccess: function (j) {
              $.showModal({
                title: '',
                message: j.data,
                onHide: function () {
                    // window.location = Urls.resolve('site_index');
                }
              });
            },
            onError: function () {
              disableSubmitButtons(false);
            }
          });

      });
});